import Vue from 'vue'

//通用打印
Vue.prototype.$log = (tip, content) => {
	if (Vue.prototype.$env === 1) {
		return;
	}
	let ttype = typeof tip; //获取tip类型
	let ctype = typeof content; //获取content类型
	let platform = uni.getSystemInfoSync().platform;
	//console.log(platform);
	if (content == undefined || content == null) {
		if (platform === 'devtools') {
			//devtools代表开发者工具 不需要解析为JSON字符串
			console.log(tip);
		} else {
			console.log(ttype === 'string' ? tip : JSON.stringify(tip));
		}
	} else {
		if (platform === 'devtools') {
			console.log(tip, content);
		} else {
			console.log((ttype === 'string' ? tip : JSON.stringify(tip)) + "->", ctype === 'string' ? content : JSON
				.stringify(content));
		}

	}
}
//通用弹窗
Vue.prototype.$pop = (tip, content) => {
	return new Promise((resolve, reject) => {
		if (Vue.prototype.$env === 1) {
			resolve();
			return;
		}
		let ttype = typeof tip; //获取tip类型
		if (!content) {
			content = ttype === 'string' ? tip : JSON.stringify(tip);
			tip = "提示信息"
		}
		ttype = typeof tip; //再获取tip类型
		let ctype = typeof content;
		uni.showModal({
			title: ttype === 'string' ? tip : JSON.stringify(tip),
			content: ctype === 'string' ? content : JSON.stringify(content), //必须是字符串
			showCancel: false,
			success: res => {
				if (res.confirm) {
					resolve();
				}
			}
		});
	})
}

//选择图片
Vue.prototype.$chooseImg=(count=1)=>{
	return new Promise((resolve,reject) => {
		uni.chooseImage({
			count: count, //默认9
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album'], //从相册选择
			success: function (res) {
				console.log(JSON.stringify(res.tempFiles));
				let tempFiles = res.tempFiles.map(item=>{
					return {
						path: item.path,
						size: item.size,
						name: item.name,
						type:item.type
					}
				})
				resolve(tempFiles);
			},
			fail(err){
				reject(err);
			}
		});
	})
}

// 预览图片
Vue.prototype.$preView = (item) =>{
	return new Promise((resolve,reject)=>{
		uni.previewImage({
			current: item,
			urls: [item],
			longPressActions: {
				itemList: ['发送给朋友', '保存图片', '收藏'],
				success: res => {
					console.log("预览图片",res);
					resolve(res);
				
				},
				fail: err=> {
					console.log("预览错误".err);
					reject(err);
				}
			}
		});
	})
}

Vue.prototype.$addUnit=(value = 'auto')=>{
    value = String(value);
	// 用uView内置验证规则中的number判断是否为数值
    return Vue.prototype.$u.test.number(value) ? `${value}rpx` : value;
}